﻿Imports System.Reflection
Imports System.Windows.Forms
Imports System.ComponentModel


Public Class PTIC_SplitContainer
    Inherits Windows.Forms.SplitContainer

#Region "Déclarations API"

    Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As IntPtr, ByVal hRgn As IntPtr, ByVal bRedraw As Boolean) As IntPtr
    Public Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
    Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Integer, ByVal hDC As Integer) As Integer

#End Region

#Region "Enumérations"
    Public Enum nomThemeSplitContainer
        OMANE
        Standard
    End Enum
#End Region

#Region "Variables privées"
    Private _theme As ThemeComposant
    Private _nomTheme As nomThemeSplitContainer
    Private _OMANEborderPanel1 As Boolean
    Private _OMANEborderPanel2 As Boolean
#End Region

#Region "Propriétés"
    <Browsable(True), Category("Thème")> _
    Public Property theme() As nomThemeSplitContainer

        Get
            Return _nomTheme
        End Get

        Set(ByVal value As nomThemeSplitContainer)

            '_theme = New ThemeComposant(value, etatComposant.Enabled)
            Dim etat As etatComposant
            'Dans la version actuelle, les thèmes ne gèrent pas les changements d'état du composant
            If Me.Enabled Then etat = etatComposant.Enabled Else etat = etatComposant.Disabled

            _nomTheme = value

            If value = nomThemeSplitContainer.OMANE Then

                Dim styleOMANEsplitContainerSTD As _
                        New styleEtatComposant(Me.BackColor, _
                                               Me.Font, _
                                               Me.ForeColor, _
                                               My.Settings.OMANE_ColorBorder, _
                                               My.Settings.OMANE_AngleRoundRect)
                Dim styleOMANEsplitContainerMOUSEOVER As _
                        New styleEtatComposant(Me.BackColor, _
                                               Me.Font, _
                                               Me.ForeColor, _
                                               My.Settings.OMANE_ColorBorder, _
                                               My.Settings.OMANE_AngleRoundRect)
                Dim styleOMANEsplitContainerDIS As _
                        New styleEtatComposant(Me.BackColor, _
                                               Me.Font, _
                                               Me.ForeColor, _
                                               My.Settings.OMANE_ColorBorder, _
                                               My.Settings.OMANE_AngleRoundRect)

                _theme = New ThemeComposant(styleOMANEsplitContainerSTD, _
                                            styleOMANEsplitContainerMOUSEOVER, _
                                            styleOMANEsplitContainerDIS, etat)

            End If

        End Set

    End Property
    <Browsable(True), Category("Thème OMANE")> _
    Public Property OMANEborderPanel1() As Boolean
        Get
            Return _OMANEborderPanel1
        End Get
        Set(ByVal value As Boolean)
            _OMANEborderPanel1 = value
            Me.Invalidate()
        End Set
    End Property
    <Browsable(True), Category("Thème OMANE")> _
    Public Property OMANEborderPanel2() As Boolean
        Get
            Return _OMANEborderPanel2
        End Get
        Set(ByVal value As Boolean)
            _OMANEborderPanel2 = value
            Me.Invalidate()
        End Set
    End Property
#End Region

#Region "Création d'un instance"
    Public Sub New()

        'Activation de la transparence
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)

        'Activation du double buffering sur le splitContainer
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.ResizeRedraw, True)

        'Activer du double buffering sur les panels enfants à l'aide de la reflection
        Dim mi As MethodInfo
        Dim ctrl As New Control
        'Dim s As String = Type.GetType("Control").Name
        mi = ctrl.GetType.GetMethod("SetStyle", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim args() As Object = New Object() {ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer, True}
        mi.Invoke(Me.Panel1, args)
        mi.Invoke(Me.Panel2, args)

        'Définition du style à appliquer au splitContainer
        theme = nomThemeSplitContainer.OMANE
        _OMANEborderPanel1 = False
        _OMANEborderPanel2 = False

        'A compléter lors de l'évolution du composant (ajout des bordures)

    End Sub
#End Region

#Region "Redéfinition des méthodes de dessin"
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        If _nomTheme = nomThemeSplitContainer.OMANE Then

            BorderStyle = Windows.Forms.BorderStyle.None
            MyBase.OnPaint(e)
            Dim g As Graphics = e.Graphics

            If Orientation = Windows.Forms.Orientation.Vertical Then
                If _OMANEborderPanel1 Then
                    g.DrawLine(New Pen(_theme.styleEnCours.borderColor), _
                               Panel1.Bounds.Width + Panel1.Bounds.X, _
                               0, _
                               Panel1.Bounds.Width + Panel1.Bounds.X, _
                               ClientRectangle.Height)
                End If
                If _OMANEborderPanel2 Then
                    g.DrawLine(New Pen(_theme.styleEnCours.borderColor), _
                               Panel1.Bounds.Width + Panel1.Bounds.X + SplitterWidth - 1, _
                               0, _
                               Panel1.Bounds.Width + Panel1.Bounds.X + SplitterWidth - 1, _
                               ClientRectangle.Height)
                End If
            Else
                If _OMANEborderPanel1 Then
                    g.DrawLine(New Pen(_theme.styleEnCours.borderColor), _
                               0, _
                               Panel1.Bounds.Height + Panel1.Bounds.Y, _
                               Panel1.Width, _
                               Panel1.Bounds.Height + Panel1.Bounds.Y)
                End If
                If _OMANEborderPanel2 Then
                    g.DrawLine(New Pen(_theme.styleEnCours.borderColor), _
                               0, _
                               Panel1.Bounds.Height + Panel1.Bounds.Y + SplitterWidth - 1, _
                               Panel1.Width, _
                               Panel1.Bounds.Height + Panel1.Bounds.Y + SplitterWidth - 1)
                End If
            End If
        Else
            MyBase.OnPaint(e)
        End If

    End Sub
#End Region

    'A intégrer dans une version ultérieure
    Public Sub definit_RegionPanels()

        If _nomTheme = nomThemeSplitContainer.OMANE Then

            Dim hDC_panel1 As IntPtr = GetWindowDC(Panel1.Handle)
            Dim g_panel1 As Graphics = Graphics.FromHdc(hDC_panel1)
            Dim hDC_panel2 As IntPtr = GetWindowDC(Panel2.Handle)
            Dim g_panel2 As Graphics = Graphics.FromHdc(hDC_panel2)

            Dim myNewRegionPanel1 As New Region(New Rectangle(1, _
                                                              1, _
                                                              Panel1.Width - 2, _
                                                              Panel1.Height - 2))
            SetWindowRgn(Me.Panel1.Handle, myNewRegionPanel1.GetHrgn(g_panel1), True)
            Dim myNewRegionPanel2 As New Region(New Rectangle(1, _
                                                              1, _
                                                              Panel2.Width - 2, _
                                                              Panel2.Height - 2))
            SetWindowRgn(Me.Panel2.Handle, myNewRegionPanel2.GetHrgn(g_panel2), True)

            g_panel1.Dispose()
            g_panel2.Dispose()
            ReleaseDC(Panel1.Handle, hDC_panel1)
            ReleaseDC(Panel2.Handle, hDC_panel2)

        End If

    End Sub

End Class
